<script setup>
let $props = defineProps(["news", "type"]);
import { computed } from "vue";

let title = computed(() => {
  switch ($props.type) {
    case "csdn":
      return "CSDN热搜";
    case "zhihu":
      return "知乎热搜";
    case "toutiao":
      return "今日头条热搜";
    case "weibo":
      return "微博热搜";
  }
});

const handleUrl = (e) => {
  let element = e.target;
  if (element.localName === "span") {
    let text = element.innerText;
    console.log("span", e.target.dataset);
    let url;
    switch ($props.type) {
      case "csdn":
        url = e.target.dataset.url;
        window.open(url);
        break;
      case "zhihu":
        window.open(`https://www.zhihu.com/search?q=${text}&type=content`);
        break;
      case "toutiao":
        url = e.target.dataset.url;
        window.open(url);
        break;
      case "weibo":
        window.open(`https://s.weibo.com/weibo?q=${text}&Refer=new_time`);
        break;
    }
  }
};
</script>

<template>
  <div class="newsItem">
    <div class="header" :class="type">
      <svg-icon name="hot2"></svg-icon>
      <span>{{ title }}</span>
    </div>
    <div class="news">
      <ul v-if="news.length > 0" @click="handleUrl">
        <li v-for="(newsitem, index) in news" :key="newsitem.id">
          <span class="index">{{ index + 1 }}</span>
          <span :data-url="newsitem.articleDetailUrl || newsitem.Url">{{
            newsitem.articleTitle ||
            newsitem.query_display ||
            newsitem.note ||
            newsitem.Title
          }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<style scoped lang="scss">
.newsItem {
  flex: 1;
  min-width: 250px;
  margin-right: 30px;
  .header {
    display: inline-block;
    text-align: center;
    width: 100%;
    height: 78px;
    position: relative;
    overflow: hidden;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    span {
      color: #fff;
      font-size: 16px;
      font-weight: 700;
      position: absolute;
      top: 40%;
      left: 0;
      right: 0;
      bottom: 0;
    }
    :deep svg {
      margin-top: 20px;
      width: 100% !important;
      height: 100% !important;
    }
    background-color: #4e6ef2;
    background-image: radial-gradient(circle at 50% -45%, #53a7f5, #4e6ef2);
    &.weibo {
      background-color: #d52c2b;
      background-image: radial-gradient(circle at 50% 3%, #d63736, #d52c2b);
    }
    &.csdn {
      background-color: #fc5632;
      background-image: radial-gradient(circle at 50% 3%, #f07a56, #fc5632);
    }
    &.toutiao {
      background-color: #d52c2b;
      background-image: radial-gradient(circle at 50% 3%, #d63736, #d52c2b);
    }
  }
  ul {
    margin-top: 10px;
    li {
      margin-bottom: 20px;
      font-size: 13px;
      height: 100%;
      padding: 5px;
      cursor: pointer;
      &:hover {
        background-color: rgb(238, 243, 243);
      }
      &:first-child {
        .index {
          background-color: red;
        }
      }
      &:nth-child(2) {
        .index {
          background-color: #f60;
        }
      }
      &:nth-child(3) {
        .index {
          background-color: #faa90e;
        }
      }
      .index {
        text-align: center;
        line-height: 20px;
        display: inline-block;
        width: 20px;
        height: 20px;
        background-color: #7f7f8c;
        color: #fff;
        margin-right: 10px;
      }
    }
  }
}
</style>
